﻿using System.Collections.Generic;
using Entitas;

public class DestroySystem : ReactiveSystem<GameEntity>
{
    public DestroySystem() : base(Contexts.sharedInstance.game)
    {
    }

    protected override ICollector<GameEntity> GetTrigger(IContext<GameEntity> context) =>
        context.CreateCollector(GameMatcher.Destroyed);

    protected override bool Filter(GameEntity entity)
    {
        return entity.isDestroyed;
    }

    protected override void Execute(List<GameEntity> entities)
    {
        foreach (var e in entities)
        {
            if (e.hasView)
            {
                e.view.viewObject.Unlink();
            }

            e.Destroy();
        }
    }
}